<template>
  <div style="margin-top: 20px">
    <el-row>
      <el-col :span=5>
        <el-input prefix-icon="el-icon-search" placeholder="请输入分类名字" v-model="searchInfo"></el-input>
      </el-col>
      <el-col :span="1" :offset=2>
        <el-button type="primary" @click="search">搜索</el-button>
      </el-col>
      <el-col :span="4" :offset="12">
        <el-button type="success" @click="addTypes">添加</el-button>
      </el-col>
    </el-row>
  </div>
  <div style="margin: 20px 0">
    <el-table v-loading="loading" :data="types" style="width: 100%" border stripe>
      <el-table-column prop="id" align="center" width="50">
      </el-table-column>
      <el-table-column prop="name" label="分类名" align="center" width="200"></el-table-column>
      <el-table-column prop="count" label="文章数量" align="center" width="100"></el-table-column>
      <el-table-column label="颜色" align="center" width="50" >
        <template #default="scope">
          <div style="width: 30px;height: 30px" :style="{'background':scope.row.color}"></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="mini" type="primary" @click="Edit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="Delete(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="countStyle">
    <el-pagination background hide-on-single-page layout="prev,pager,next" v-model:current-page="currentPage" :page-count="pages">
    </el-pagination>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  name: "Types",
  data(){
    return{
      pages: 0,
      currentPage: 0,
      loading: true,
      searchInfo: '',
      types:[],
      deleteType: {
        id: null,
        md5Password: null
      }
    }
  },
  watch: {
    currentPage(value){
      this.loading = true;
      this.getType(value)
    }
  },
  mounted(){
    this.getType(1)
  },
  methods: {
    getType(currentPage){
      axios.get("/admin/listTypes",{params:{currentPage: currentPage,pageSize:10}}).then( (res) => {
        this.types = res.data.records
        this.pages = res.data.pages
        this.loading = false
      })
    },
    addTypes: function (){
      this.$router.replace({name:'addTypes'});
    },
    Edit: function (index){
        this.$router.push({
          name: 'addTypes',
          query: {
            id: index
          }
        })
    },
    search: function (){
      axios.get("/admin/searchType",{params:{searchInfo: this.searchInfo}}).then( (res) => {
        this.types = res.data
      })
    },
    Delete(index,row){
      this.$confirm('此操作不可逆,是否继续','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        text: 'warning'
      }).then(() => {
        this.deleteType.id = row.id;
        this.types.splice(index,1);
        this.deleteType.md5Password = localStorage.getItem("password");
        if ( row.count === 0) {
          axios.post("/admin/deleteType", this.deleteType).then((res) => {
            if (res.data === true) {
              this.$message({
                type: 'success',
                message: '删除成功'
              })
            } else
              this.$message.error("删除失败")
          })
        }
        else {
          this.$message.error("删除失败，因为存在有文章使用该分类")
        }
      }).cache(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>